﻿using System.Web.UI.WebControls;
using System;
using System.Web;
using System.Collections.Specialized;
using HNAlbum.Helper;

namespace HNAlbum.Component.AlbumGallery
{
    public class DashboardPager : Panel
    {
        /// <summary>
        /// Default constructor, create the pager without going Back button
        /// </summary>
        /// <param name="itemPerPage">Number of items per page</param>
        /// <param name="totalItems">Total number of items in the dashboard</param>
        /// <param name="pageIndex">Page to be displayed</param>
        public DashboardPager(int itemPerPage, int totalItems, int pageIndex)
        {
            this.Attributes.Add("id", "album_dashboard_pager");
            int pageCount = Function.GetPageCount(itemPerPage, totalItems);
            if (pageIndex > pageCount) pageIndex = pageCount;

            //Loop and create paging buttons
            for (int i = 1; i <= pageCount; i++)
            {
                //Pager button
                Panel pagerButton = new Panel();

                #region Process pager button Url

                //Page URL
                string currentPageUrl = HttpContext.Current.Request.Url.AbsolutePath;

                //Refine query string
                NameValueCollection originalQueryString = new NameValueCollection(HttpContext.Current.Request.QueryString);
                originalQueryString.Remove("page");
                //Final query string, without "page" key
                string finalQueryString = "?";
                //Rebuild the query string
                if (originalQueryString.Count > 0)
                {
                    for (int j = 0; j < originalQueryString.Count; j++)
                    {
                        finalQueryString += originalQueryString.Keys[j] + "=" + originalQueryString[j] + "&";
                    }
                    finalQueryString += "page=" + i.ToString();
                }
                else
                {
                    finalQueryString = "?page=" + i.ToString();
                }
                #endregion
                //Now assign onlick event with built URL
                pagerButton.Attributes.Add("onclick", string.Format("window.location.href='{0}{1}'", currentPageUrl, finalQueryString));

                this.Controls.Add(pagerButton);

                //Pager button label
                Label lblCount = new Label();
                pagerButton.Controls.Add(lblCount);

                if (i == pageIndex)
                {
                    pagerButton.Attributes.Add("class", "selected button");
                    lblCount.Text = i.ToString();
                }
                else
                {
                    pagerButton.Attributes.Add("class", "button");
                    lblCount.Text = "■";
                }
            }
        }

        /// <summary>
        /// Create pager with go back button
        /// </summary>
        /// <param name="itemPerPage">Number of items per page</param>
        /// <param name="totalItems">Total number of items in the dashboard</param>
        /// <param name="pageIndex">Page to be displayed</param>
        /// <param name="backUrl">The URL to be navigated to when Back button is click</param>
        public DashboardPager(int itemPerPage, int totalItems, int pageIndex, string backUrl)
            : this(itemPerPage, totalItems, pageIndex)
        {
            Panel backButton = new Panel();
            backButton.Attributes.Add("onclick", string.Format("window.location.href='{0}'", backUrl));
            backButton.Attributes.Add("class", "back selected button");
            this.Controls.AddAt(0, backButton);

            Label lblBack = new Label();
            lblBack.Text = "Back to Home";
            backButton.Controls.Add(lblBack);
        }
    }
}